<template>
	<view class="midPageDL login-btn">
		<!-- 这个地方做一个中间页面 直接下单签约的页面 做一个操作就好了-->
		<button type="primary" style="width: 600rpx;" @click="jump(midPageData.url)">跳转签约</button>
	</view>


</template>

<script setup>
	import {
		reactive,
		onUnmounted,
		nextTick,
	} from 'vue';
	import { storeToRefs } from 'pinia';
	import {
		onLoad,
		onPageScroll
	} from '@dcloudio/uni-app';
	import initApi from '@/apis/init.js';
	import { useUserStore } from '@/store/user.js';
	const { orderTimeMid, midPageData } = storeToRefs(useUserStore());
	const formData = reactive({
		mobile: '',
		smsCode: null,
		orderNo: null,
		channelCode: '',
		productCode: '',
		allChecked: false,
		agreeType: 1,
		showHeight: 0,
		url: '',
		timer: '',
		timeS: 0
	});
	// 时间计算
	const moreThan20s = lastOrderTime => {
		if (!lastOrderTime) {
			return false;
		}
		const currentTime = getTime();
		return (currentTime - lastOrderTime) > 15 * 1000; // 3 minutes in milliseconds
	};


	const timer = async () => {
		if (!midPageData.value.orderNo) return;
		const res = await initApi.orderDetail({ orderNo: midPageData.value.orderNo });
		// 清理数据

		if (res.code == 20000) {
			midPageData.value.orderNo = '';
			midPageData.value.url = '';
			midPageData.value.orderNo = '';
			midPageData.value.orderTime = '';
			orderTimeMid.value = '';

			/*   
			  "1": "初始",
			  "2": "签约",
			  "3": "解约",
			  "4": "退款"
			*/
			if (res.data.orderStatus == 2) {
				window.location.href =
					'https://flycard.cscmgg.com/pages/H5-mobile/stock/coce2844_PrlQYh?channelCode=uxQEVDxp&partyId=1&status=1';
			} else {
				window.location.href =
					'https://flycard.cscmgg.com/pages/H5-mobile/stock/coce2844_PrlQYh?channelCode=uxQEVDxp&partyId=1';
			}
		} else {
			midPageData.value.orderNo = '';
			midPageData.value.url = '';
			midPageData.value.orderNo = '';
			midPageData.value.orderTime = '';
			orderTimeMid.value = '';
			window.location.href =
				'https://flycard.cscmgg.com/pages/H5-mobile/stock/coce2844_PrlQYh?channelCode=uxQEVDxp&partyId=1';

		}

	};
	onLoad(query => {

		let intervalIdMid = setInterval(() => {
			formData.timeS++;
			// 这个直接就是 7s 过后查询
			if (formData.timeS >= 5) {
				clearInterval(intervalIdMid); // 清除定时器
				timer();
				return;
			}
		}, 1000);
		if (midPageData.value.orderNo) {
			// 上一次没完成 继续来
			if (moreThan20s(midPageData.value.orderTime)) {
				midPageData.value.url = query;
				if (query.orderNo) {
					midPageData.value.orderNo = query.orderNo;
				}
				if (query.orderTime) {
					midPageData.value.orderTime = query.orderTime;
					orderTimeMid.value = query.orderTime;
				}
				jump(query);
				return;
			}
			return;
		} else {
			// 保证第一次 完美跳转
			midPageData.value.url = query;
			if (query.orderNo) {
				midPageData.value.orderNo = query.orderNo;
			}
			if (query.orderTime) {
				midPageData.value.orderTime = query.orderTime;
				orderTimeMid.value = query.orderTime;
			}
			jump(query);
			return;
		}



	});
	const jump = query => {
		window.location.href = query.url + '&session_id=' + query.session_id;
	};
</script>

<style lang="scss">
	.midPageDL {
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 750rpx;
	}
</style>